

/*
// 读取文件内容
function readFile(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = (event) => {
      resolve(event.target.result);
    };
    reader.onerror = (event) => {
      reject(event.target.error);
    };
    reader.readAsArrayBuffer(file);
  });
}
*/
/*
// 计算哈希256
function calculateHash(buffer) {
  return crypto.subtle.digest("SHA-256", buffer);
}
*/
/*
// 处理文件选择事件
function handleFileSelect(event) {
  const file = event.target.files[0];
  if (file) {
    readFile(file)
      .then((buffer) => calculateHash(buffer))
      .then((hash) => {
        const hashArray = Array.from(new Uint8Array(hash));
        const hashHex = hashArray
          .map((byte) => byte.toString(16).padStart(2, "0"))
          .join("");

        textarea_hex256.innerHTML = hashHex;   
        fztz1.innerHTML = '请将文件拖拽到这里'    // 将拖拽区文字 还原
        // console.log("Hash:", hashHex);
      })
      .catch((error) => {
        console.error("Error:", error);
      });
  }
}
*/
/*
// 处理文件选择事件
function handleFileSelect1(event) {
  const file = event.target.files[0];
  if (file) {
    readFile(file)
      .then((buffer) => calculateHash(buffer))
      .then((hash) => {
        const hashArray = Array.from(new Uint8Array(hash));
        const hashHex = hashArray
          .map((byte) => byte.toString(16).padStart(2, "0"))
          .join("");
        textarea_hex256_1.innerHTML = hashHex;
        fztz2.innerHTML = '请将文件拖拽到这里'    // 将拖拽区文字 还原
        // console.log("Hash:", hashHex);
      })
      .catch((error) => {
        console.error("Error:", error);
      });
  }
}
*/
let is_gavei1 = 0 //  左边
let is_gavei2 = 0

// JavaScript代码：  单个文件上传  及响应值
function uploadFile() {
  is_gavei1 = 0
  const fileInput = document.getElementById('file_input');
  const file = fileInput.files[0];
  if (file) {
    const formData = new FormData();
    formData.append('file', file);
    fetch('/upload', {
      method: 'POST',
      body: formData
    })
      .then(response => response.json())
      .then(data => {
        textarea_hex256.innerHTML = data.hash;
        fztz1.innerHTML = '请将文件拖拽到这里'    // 将拖拽区文字 还原
        // console.log('服务器返回的数据:', data);
      })
      .catch(error => {
        console.error('上传文件时发生错误:', error);
      });
  }
}
// JavaScript代码：  单个文件上传  及响应值
function uploadFile1() {
  is_gavei2 = 0
  const fileInput = document.getElementById('file_input1');
  const file = fileInput.files[0];
  if (file) {
    const formData = new FormData();
    formData.append('file', file);
    fetch('/upload', {
      method: 'POST',
      body: formData
    })
      .then(response => response.json())
      .then(data => {
        textarea_hex256_1.innerHTML = data.hash;
        fztz2.innerHTML = '请将文件拖拽到这里'    // 将拖拽区文字 还原
        // console.log('服务器返回的数据:', data);
      })
      .catch(error => {
        console.error('上传文件时发生错误:', error);
      });
  }
}



bidui_btn.onclick = () => {

  bd_result.innerHTML = "";
  let textarea = textarea_hex256.value;
  let textarea1 = textarea_hex256_1.value;
  // console.log('textarea= ', textarea)
  // console.log('textarea1= ', textarea1)
  if (textarea != "" && textarea1 != "") {
    if (textarea === textarea1) {
      bd_result.innerHTML = "两个文件的哈希256值完全相同";
      bd_result.style.color = "black";
      is_gavei2 = 0
      is_gavei1++
      // document.getElementById('bd_result').classList.remove('zoomed')
    } else {
      bd_result.innerHTML = "两个文件的哈希256值不相同";
      bd_result.style.color = "red";
      is_gavei1 = 0
      is_gavei2 ++
    }
  }
  if (is_gavei1>= 2 || is_gavei2 >= 2) {
    document.getElementById('bd_result').classList.add('zoomed')
    let a = setInterval(()=>{
      document.getElementById('bd_result').classList.remove('zoomed')
      console.log('is_gavei1=',is_gavei1)
      console.log('is_gavei2=',is_gavei2)
      clearTimeout(a)
    },300)

  }
  return "";
};

// 监听文件选择事件
const fileInput = document.getElementById("file_input");
fileInput.addEventListener("change", uploadFile);
const fileInput1 = document.getElementById("file_input1");
fileInput1.addEventListener("change", uploadFile1);

//  ------------------fztz1-----------------
fztz1.ondragenter = function (e) {
  e.preventDefault();
  e.stopPropagation();
  // console.log("进入");
}
//   在上面 
fztz1.ondragover = function (e) {
  e.preventDefault();   //阻止默认事件
  // e.stopPropagation();  //阻止冒泡
  // console.log("在上面");
}
//  ------------------fztz2-----------------
fztz2.ondragenter = function (e) {
  e.preventDefault();
  e.stopPropagation();
  // console.log("进入");
}
//   在上面 
fztz2.ondragover = function (e) {
  e.preventDefault();   //阻止默认事件
  // e.stopPropagation();  //阻止冒泡
  // console.log("在上面");
}

// 放下--------------上传单个文件------------------------------
fztz1.ondrop = function (e) {
  is_gavei1 = 0
  e.preventDefault();  //阻止默认事件
  e.stopPropagation();  //阻止冒泡
  const file = e.dataTransfer.files[0]
  fztz1.innerHTML = file.name

  uploadFilels(file,1)      //  区分fztz1
  // console.log("放下=", file);
}

fztz2.ondrop = function (e) {
  is_gavei2 = 0
  e.preventDefault();  //阻止默认事件
  e.stopPropagation();  //阻止冒泡
  const file2 = e.dataTransfer.files[0]
  fztz2.innerHTML = file2.name      // 

  uploadFilels(file2,2)            //  区分fztz2
  // console.log("放下=", file2);
}

function uploadFilels(file,tt) {

  let formData = new FormData();
  formData.append('file', file)
  fetch('/upload', {
    method: "POST",
    body: formData
  })
    .then(response => response.json())
    .then(data => {
      if (tt == 1) {
      textarea_hex256.innerHTML = data.hash;
      }else if (tt == 2) {
        textarea_hex256_1.innerHTML = data.hash;
      }
      // console.log('服务器返回的数据', data)
    })
    .catch(error => {
      console.log("上传文件时发生错误", error)
    })
}